﻿using Microsoft.EntityFrameworkCore;

namespace TuLing.Blazor.Services
{
    public class BaseService
    {
        protected readonly DefaultDbContext _context;
        public BaseService(DefaultDbContext context)
        {
            _context = context;
        }

        public void ResetOriginalData()
        {
            foreach (var entry in _context.ChangeTracker.Entries())
            {
                switch (entry.State)
                {
                    case EntityState.Modified:
                        entry.CurrentValues.SetValues(entry.OriginalValues);
                        entry.State = EntityState.Unchanged;
                        break;
                    case EntityState.Added:
                        entry.State = EntityState.Detached;
                        break;
                    case EntityState.Deleted:
                        entry.State = EntityState.Unchanged;
                        break;
                }
            }
        }
    }
}
